package com.yutel.gps.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	public static String en2gbk(String en) {
		DateFormat df = new SimpleDateFormat("yyyymm");
		DateFormat dd = new SimpleDateFormat("yyyy年mm月");
		try {
			Date d = df.parse(en);
			return dd.format(d);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static Date Str2Date(String day) {
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		try {
			return df.parse(day);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String curDate() {
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
		try {
			return df.format(new Date());
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 在几天之后
	 * 
	 * @param date
	 * @param day天数
	 * @return
	 */
	public static boolean xDayAfter(Date date, int day) {
		if (date == null)
			return true;
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.DAY_OF_YEAR, -day);
		Date curDay = cal.getTime();
		return curDay.after(date);

	}

	public static boolean inTime(int start, int end) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		int curtime = cal.get(Calendar.HOUR_OF_DAY);
		return (curtime > start) && (curtime < end);

	}

	public static String timestamp() {
		return "" + System.currentTimeMillis();
	}

	public static void main(String[] args) {
		System.out.println(en2gbk("201108"));
		System.out.println(xDayAfter(null, 5));
		System.out.println(xDayAfter(Str2Date("20151220"), 5));
		System.out.println(xDayAfter(Str2Date("20151221"), 5));
		System.out.println(xDayAfter(Str2Date("20151222"), 5));
	}

}
